package com.scirillo.frases.service;

import java.util.Calendar;

import com.scirillo.frases.preference.FrasesPreferenceManager;
import com.scirillo.frases.util.Constants;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class AlarmService {
    private Context context;
    private PendingIntent mAlarmSender;
    public AlarmService(Context context) {
        this.context = context;
        mAlarmSender = PendingIntent.getBroadcast(context, 0, new Intent(context, AlarmReceiver.class), 0);
    }

    public void startAlarm(){
        Calendar c = Calendar.getInstance();
        if((FrasesPreferenceManager.GetPTPreferencesInt(Constants.NOTIFICATION_TIME_MIN, 0) > 0) && 
        		(FrasesPreferenceManager.GetPTPreferencesInt(Constants.NOTIFICATION_TIME_MIN, 0) > 0)){
            c.add(Calendar.MINUTE, FrasesPreferenceManager.GetPTPreferencesInt(Constants.NOTIFICATION_TIME_MIN, 0));
            c.add(Calendar.HOUR, FrasesPreferenceManager.GetPTPreferencesInt(Constants.NOTIFICATION_TIME_HS, 0));            
        }
        c.add(Calendar.SECOND, 10);    
        c.add(Calendar.DAY_OF_YEAR, 1);
        long firstTime = c.getTimeInMillis();
        // Schedule the alarm!
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 10000, mAlarmSender);
    }
}